package com.panfeng.xcloud.scheduler.util;

import org.springframework.util.Assert;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * @author Jason
 * @date 2019/5/21 15:42
 */
public class DateUtil {

	public static String FORMAT_YYYYMMDD = "yyyyMMdd";
	public static String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
	public static String FORMAT_YY_MM_DD = "yyMMdd";
	public static String FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static String FORMAT_IOS = "yyyy/MM/dd HH:mm:ss";
	public static String FORMAT_MINUTE = "yyyy-MM-dd HH:mm";

	public static Date getNow() {
		return new Date(System.currentTimeMillis());
	}

	public static long getNowTimestamp() {
		return getNow().getTime();
	}

	public static final String toMailDateDtPartString(Date aDate) {
		return toFormatDateString(aDate, "yyyyMMdd");
	}

	public static final String toMailDateString(Date aDate) {
		return toFormatDateString(aDate, "yyyyMMddHHmmss");
	}

	public static final String toFullDateCompactString(Date aDate) {
		return toFormatDateString(aDate, "yyyyMMddHHmmssSSS");
	}

	public static final String toFormatDateString(Date aDate, String formatStr) {
		if (aDate == null) {
			return "";
		}
		Assert.hasText(formatStr);
		return new SimpleDateFormat(formatStr).format(aDate);
	}

	/**
	 * yyyy-MM-dd HH:mm:ss格式
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToStr(Date date) {
		return new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS).format(date);
	}

	/**
	 * 获取yyyy-MM-dd格式
	 * 
	 * @param date
	 * @return
	 */
	public static String getStringByDate(Date date) {
		return new SimpleDateFormat(FORMAT_YYYY_MM_DD).format(date);
	}

	/**
	 * 获取yyMMdd格式
	 * 
	 * @param date
	 * @return
	 */
	public static String getYYMMDDStringByDate(Date date) {
		return new SimpleDateFormat(FORMAT_YY_MM_DD).format(date);
	}

	/**
	 * 获取本周五日期YYMMDD格式
	 * 
	 * @param date
	 * @return
	 */
	public static String getFridayDate() {
		SimpleDateFormat formater = new SimpleDateFormat(FORMAT_YY_MM_DD);
		Calendar cal = new GregorianCalendar();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 4);
		Date last = cal.getTime();
		return formater.format(last);
	}

	/**
	 * 获取本周五日期FORMAT_YYYY_MM_DD格式
	 * 
	 * @param date
	 * @return
	 */
	public static String getFridayDateYYYYMMDD() {
		SimpleDateFormat formater = new SimpleDateFormat(FORMAT_YYYY_MM_DD);
		Calendar cal = new GregorianCalendar();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 4);
		Date last = cal.getTime();
		return formater.format(last);
	}

	/**
	 * 在提供的date的基础上添加days，返回格式为yyyy-MM-dd
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static String addDays(Date date, int days) {
		return addDays(date, days, FORMAT_YYYY_MM_DD);
	}

	/**
	 * 在提供的date的基础上添加days，返回格式为yy-MM-dd
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static String addYYMMDDDays(String strDate, int days) {
		try {
			DateFormat format = new SimpleDateFormat(FORMAT_YY_MM_DD);
			Date date = format.parse(strDate);
			return addDays(date, days, FORMAT_YY_MM_DD);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String addDays(Date date, int days, String dateFromat) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DATE, days);
			return parseDateToStr(calendar.getTime(), dateFromat);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 格式化Date时间
	 * 
	 * @param time       Date类型时间
	 * @param timeFromat String类型格式
	 * @return 格式化后的字符串
	 */
	public static String parseDateToStr(Date time, String timeFromat) {
		return new SimpleDateFormat(timeFromat).format(time);
	}

	/**
	 * 获取过期时间
	 * 
	 * @param date
	 * @param time
	 * @return
	 */
	public static String getExpiryTime(Date date, Long time) {
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
		Date afterDate = new Date(date.getTime() + time);
		return sdf.format(afterDate);
	}

	public static Date strToDateLong(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	public static boolean compareDate(Date date1, Date date2) {
		long beginMillisecond = date1.getTime();
		long endMillisecond = date2.getTime();
		if (beginMillisecond > endMillisecond) {
			return true;
		}
		return false;
	}

	public static Long formateUTCTimeToLong(String time) {
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");// 注意格式化的表达式
			time = time.replace("Z", " UTC");// 注意是空格+UTC
			Date date = formatter.parse(time);
			return date.getTime() / 1000;
		} catch (ParseException e) {
			e.printStackTrace();
			return System.currentTimeMillis() / 1000;
		}
	}

	public static String formateUTCTime(String time) {
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");// 注意格式化的表达式
			time = time.replace("Z", " UTC");// 注意是空格+UTC
			return dateToStr(formatter.parse(time));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getISO8601Timestamp(Date date) {
		TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
		df.setTimeZone(tz);
		String nowAsISO = df.format(date);
		return nowAsISO;
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param startDate 较小的时间
	 * @param endDate   较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date startDate, Date endDate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		startDate = sdf.parse(sdf.format(startDate));
		endDate = sdf.parse(sdf.format(endDate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(startDate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(endDate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	public static void main(String[] args) throws ParseException {
		Date date = getNow();
		Date friday = strToDateLong(getFridayDateYYYYMMDD() + " 16:00:00");
		if (compareDate(date, friday)) {
			System.out.println(addDays(date, 7, FORMAT_YY_MM_DD));
		} else {
			System.out.println(getFridayDate());
		}
	}

}
